﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using CultuRAM.Models;

namespace CultuRAM.DAL
{
    public class CultuRAMContext : DbContext
    {
        public CultuRAMContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<webpages_Membership> Memberships { get; set; }
        public DbSet<webpages_Roles> Roles { get; set; }
        public DbSet<webpages_UsersInRoles> UserInRoles { get; set; }

        public DbSet<Event> Events { get; set; }
        public DbSet<EventType> EventTypes { get; set; }
        public DbSet<EventProperty> EventProperties { get; set; }
        public DbSet<EventPropertyValue> EventPropertyValues { get; set; }
        public DbSet<Place> Places { get; set; }
        public DbSet<Session> Sessions { get; set; }
        public DbSet<Participant> Participants { get; set; }
        public DbSet<ParticipantType> ParticipantTypes { get; set; }
        public DbSet<EventParticipant> EventParticipants { get; set; }
        public DbSet<Resource> Resources { get; set; }
        public DbSet<ResourceType> ResourceTypes { get; set; }
        public DbSet<Comment> Comments { get; set; }  
        public DbSet<Entity> Entities { get; set; }
        public DbSet<EntityType> EntityTypes { get; set; }
        public DbSet<City> Cities { get; set; }
        public DbSet<AccreditingRequest> AccreditingRequests { get; set; }
        public DbSet<AccreditingState> AccreditingStates { get; set; }
        public DbSet<EventState> EventStates { get; set; }
        public DbSet<Target> Targets { get; set; }
        public DbSet<Alert> Alerts { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
                base.OnModelCreating(modelBuilder);

                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();


                modelBuilder.Entity<EventType>()
                            .HasKey(r => r.TypeId)
                            .HasRequired(q => q.SuperType)
                            .WithOptional()
                            .Map(t => t.MapKey("SuperTypeId"));

                modelBuilder.Entity<EventType>()
                            .HasKey(k => k.TypeId)
                            .HasOptional(a => a.SuperType)
                            .WithMany()
                            .HasForeignKey(fk => fk.SuperTypeId).WillCascadeOnDelete(false);


                modelBuilder.Entity<UserProfile>()
                                .HasKey(r => r.UserId)
                                .HasRequired(q => q.Entity)
                                .WithRequiredDependent()
                                .Map(t => t.MapKey("EntityId"));

                modelBuilder.Entity<UserProfile>()
                                .HasKey(k => k.UserId)
                                .HasRequired(q => q.Entity)
                                .WithMany()
                                .HasForeignKey(fk => fk.EntityId).WillCascadeOnDelete(false);
        }
    }
}